package com.pokermadness.servlets;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.json.JSONException;

import com.pokermadness.game.Game;

@WebServlet("/GameServlet")
public class GameServlet extends GenericServlet {

	/**
	 * 
	 */
	private static final long serialVersionUID = 4214650173987653954L;

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		super.doPost(request, response);
		HttpSession session = (HttpSession) request.getAttribute("session");

		Game game = (Game) session.getAttribute("game");
		if (game == null) {
			game = new Game();
			game.startGame();
			session.setAttribute("game", game);
		}

		if (request.getParameterMap().containsKey("initGame")) {
			postGetInitGame(request, response, game);
		} else if (request.getParameterMap().containsKey("move")) {
			processPlayerMove(request, response, game);
		} else if (request.getParameterMap().containsKey("send")) {
			sendMove(request, response, game);
		} else if (request.getParameterMap().containsKey("finish")) {
			finishGame(request, response, game);
		} else if (request.getParameterMap().containsKey("end")) {
			endGame(session);
		} else if (request.getParameterMap().containsKey("isRunning")) {
			isRunning(request, response, game);
		} else if (request.getParameterMap().containsKey("initSession")) {
			response.getWriter().write("");
		}
	}

	private void postGetInitGame(HttpServletRequest request,
			HttpServletResponse response, Game game) throws IOException {
		boolean addLog;
		if (request.getParameter("sendLog").equals("true"))
			addLog = true;
		else
			addLog = false;
		response.getWriter().write(game.getInitString(addLog));
	}

	private void isRunning(HttpServletRequest request,
			HttpServletResponse response, Game game) throws IOException {
		response.getWriter().write(game.getIsRunningString());
	}

	private void processPlayerMove(HttpServletRequest request,
			HttpServletResponse response, Game game) throws IOException {
		int move = Integer.parseInt(request.getParameter("move"));
		response.getWriter().write(game.makeMove(move));
	}

	private void sendMove(HttpServletRequest request,
			HttpServletResponse response, Game game) throws IOException {
		try {
			Thread.sleep(1000);
		} catch (InterruptedException e1) {
			e1.printStackTrace();
		}
		try {
			response.getWriter().write(game.playRound().toString());
		} catch (JSONException e) {
			e.printStackTrace();
		}
	}

	private void finishGame(HttpServletRequest request,
			HttpServletResponse response, Game game) throws IOException {
		response.getWriter().write(game.finishGame());
	}

	private void endGame(HttpSession session) {
		session.removeAttribute("game");
	}

}
